<?php

class DBPersist {

    var $server = "localhost";
    var $usuario = "root";
    var $senha = "";
    var $banco = "sade-ipa";
    var $query;
    var $result;
    var $link;

    function conecta_db() {
        $this->link = mysql_connect($this->server, $this->usuario, $this->senha);
        mysql_set_charset('utf8',$this->link);
        if (!$this->link) {
            echo'Erro de conexao';
            die();
        } else if (!mysql_select_db($this->banco, $this->link)) {
            echo'Erro ao Selecionar o Banco de dados';
            die();
        }
    }

    function desconecta_db() {
        // Tip: Using mysql_close() isn't required, as non-persistent connections are
        // automatically closed at the end of the script.
        //mysql_close($this->link);
    }

    function sql_db($query) {

        //echo "Entrou no sql_db";
        $link = $this->conecta_db();
        $this->query = $query;
        //echo $query;
        $connComp = $this->result = mysql_query($this->query, $this->link);
        if ($connComp) {
            //  $this->linhas_lidas=mysql_num_rows($this->result);
            $this->linhas_lidas = mysql_affected_rows($this->link);
            $this->desconecta_db();
            return $this->result;
        } else {
            $this->erro = mysql_error();
            echo "<br />Deu erro no sql_db<br />" . $this->erro;
        }
    }

}

?>